﻿
#include "RootWindow.h"
#include "DesignViewController.h"
#include "FirstViewController.h"
#include "ImgSaveFinishViewControler.h"
#include "Filters.h"

//加载滤镜库
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32  || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
//	#pragma comment(lib, "../Filter_library/windows/Filters.lib")
//#endif

//全局变量
static char bigTemplateImage[30] = "source_pro/design/0.png";
static unsigned int  c_filter;

static unsigned short selectType = 0;

//标识变量（是否显示模板图片资源是滤镜还是构图）
static unsigned short isfilterView = 0;
//判断是否设置旋转90度宽度
static bool isSetRotionWith = false;
static bool isSaveImgRotion90 = false;

//旋转角度
static int rotionAnagle = 0;

static CAView* inputColor = NULL;
static CATextField* textField = NULL;
CAButton* fontChange_button = NULL;
CAButton* Ok_button = NULL;
CAButton* keyChange_button = NULL;
CALabel* lable = NULL;

CAActivityIndicatorView* loadingIcon = NULL;

//保存修改图片相关
std::string changeImagePath;
extern std::vector<CAImage*> UserSelectImg;

std::string ImageName;
CAImage* c_Image;

//IOS平台分享（CAImage UIimage）
CAImage* c_ImageName;

//要修改图片的比例值
extern float scale_value;

//是否显示加载图标
static bool isShowLodingIcon = false;

DesignViewController::DesignViewController() :
flag(0)
{
	//模板资源相关
	templateList.clear();
	templateList.push_back("source_pro/design/template_image_1.jpg");
	templateList.push_back("source_pro/design/template_image_2.jpg");
	templateList.push_back("source_pro/design/template_image_3.jpg");
	templateList.push_back("source_pro/design/template_image_4.jpg");
	templateList.push_back("source_pro/design/template_image_5.jpg");
	templateList.push_back("source_pro/design/template_image_6.jpg");
	templateList.push_back("source_pro/design/template_image_7.jpg");
	templateList.push_back("source_pro/design/template_image_8.jpg");
	templateList.push_back("source_pro/design/template_image_9.jpg");
	templateList.push_back("source_pro/design/template_image_10.jpg");
	templateList.push_back("source_pro/design/template_image_11.jpg");
	templateList.push_back("source_pro/design/template_image_12.jpg");

	filterLable.push_back(UTF8("原图"));
	filterLable.push_back(UTF8("经典"));
	filterLable.push_back(UTF8("黑白"));
	filterLable.push_back(UTF8("云端"));
	filterLable.push_back(UTF8("候鸟"));
	filterLable.push_back(UTF8("底片"));
	filterLable.push_back(UTF8("浮雕"));
	filterLable.push_back(UTF8("淡雅"));

	//构图资源相关
	pictureList.clear();
	pictureList.push_back("source_pro/design/backward_normal.png");
	pictureList.push_back("source_pro/design/clockwise_noramal.png");
	pictureList.push_back("source_pro/design/updown_normal.png");
	pictureList.push_back("source_pro/design/leftright_normal.png");

}


DesignViewController::~DesignViewController()
{


}

void DesignViewController::viewDidLoad()
{
	//获取边界值
	DSize Winsize = this->getView()->getBounds().size;
	//黑色底边
	CAView* bottomBackgroundColor = CAImageView::createWithColor(CAColor_black);
	bottomBackgroundColor->setFrame(DRect(0, Winsize.height*0.8, Winsize.width, Winsize.height / 3));
	this->getView()->addSubview(bottomBackgroundColor);

	//修改图片的背景颜色/图片
	CAImageView* imageView = CAImageView::createWithImage(UserSelectImg.at(1));
	imageView->setFrame(DRect(0, 0, Winsize.width, Winsize.height*0.78));
	this->getView()->addSubview(imageView);

	//渐变背景
	CAImage* shade_image = CAImage::create("source_pro/saveimage/shade.png");
	CAImageView* shade_imageView = CAImageView::createWithImage(shade_image);
	shade_imageView->setFrame(this->getView()->getBounds());
	this->getView()->addSubview(shade_imageView);

	//添加资源原图资源
	CAImageView* imageViewss = CAImageView::createWithImage(UserSelectImg.at(0));
	this->getView()->addSubview(imageViewss);

	CASegmentedControl* semented = CASegmentedControl::createWithFrame
		(DRect(0, (Winsize.height)*0.78, Winsize.width, Winsize.height / 12), 3);
	semented->setImageForSegmentAtIndex(CAImage::create("source_pro/design/moban_image_btn.png"), 0, CAControlStateNormal);
	semented->setImageForSegmentAtIndex(CAImage::create("source_pro/design/moban_image_btn_down.png"), 0, CAControlStateSelected);
	semented->setImageSizeAtIndex(DSize((Winsize.width)*0.084, (Winsize.height)*0.025), 0);

	semented->setImageForSegmentAtIndex(CAImage::create("source_pro/design/lvjing_image_btn.png"), 1, CAControlStateNormal);
	semented->setImageForSegmentAtIndex(CAImage::create("source_pro/design/lvjing_image_btn_down.png"), 1, CAControlStateSelected);
	semented->setImageSizeAtIndex(DSize((Winsize.width)*0.084, (Winsize.height)*0.025), 1);

	semented->setImageForSegmentAtIndex(CAImage::create("source_pro/design/goutu_image_btn.png"), 2, CAControlStateNormal);
	semented->setImageForSegmentAtIndex(CAImage::create("source_pro/design/goutu_image_btn_down.png"), 2, CAControlStateSelected);
	semented->setImageSizeAtIndex(DSize((Winsize.width)*0.084, (Winsize.height)*0.025), 2);

	semented->setBackgroundImage(CAImage::create("source_pro/design/bk.png"));
	semented->setSegmentItemBackgroundImage(CAImage::create("source_pro/design/bk.png"));
	semented->setTintColor(CAColor_black);

	//设置监听
	semented->addTarget(this, CASegmentedControl_selector(DesignViewController::segmentCallback));
	semented->setSelectedAtIndex(0);
	this->getView()->addSubview(semented);

	//默认列出模板相关内容
	this->getView()->removeSubview(filterlistview);
	this->getView()->removeSubview(picturelistview);
	templistview = CAListView::createWithFrame(DRect((Winsize.width)*0.03, (Winsize.height)*0.86, Winsize.width, (Winsize.height)*0.13));
	templistview->setListViewDelegate(this);
	templistview->setListViewDataSource(this);
	templistview->setAllowsSelection(true);
	templistview->setAllowsMultipleSelection(false);
	templistview->setListViewOrientation(CAListViewOrientationHorizontal);
	templistview->setShowsScrollIndicators(false);
	templistview->setSeparatorColor(CAColor_black);
	templistview->setBackgroundColor(CAColor_black);
	templistview->setSeparatorViewHeight(Winsize.width*0.018);
	this->getView()->addSubview(templistview);

	//默认列出加载的大图片
	float num = Winsize.height*0.69 / 1.5;
	showImageListview = ShowSelectedImageListView::createWithFrame
			(DRect((Winsize.width - num) / 2, Winsize.height / 15, num, (Winsize.height)*0.69));
	this->getView()->addSubview(showImageListview);

	//输入框背景颜色纯黑色
	inputColor = CAImageView::createWithColor(CAColor_black);
	inputColor->setFrame(DRect(0, (Winsize.height)*0.43, Winsize.width, Winsize.height*0.065));
	inputColor->setVisible(false);	//设置输入框隐藏
	this->getView()->addSubview(inputColor);

	//Lable
	lable = CALabel::createWithCenter(DRect(Winsize.width*0.1, Winsize.height*0.465, Winsize.width*0.2, Winsize.height*0.1));
	lable->setText("18 / 18");
	lable->setColor(CAColor_white);
	lable->setFontSize(25);
	lable->setTextAlignment(CATextAlignmentCenter);
	lable->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	lable->setVisible(false);
	this->getView()->addSubview(lable);

	//文字切换按钮
	fontChange_button = CAButton::create(CAButtonTypeSquareRect);
	fontChange_button->setCenter(DRect((Winsize.width)*0.86, (Winsize.height)*0.465, Winsize.width*0.1, Winsize.width*0.1));
	fontChange_button->setTitleColorForState(CAControlStateNormal, CAColor_white);
	fontChange_button->setBackGroundViewForState(CAControlStateNormal, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/design/font_change.png")));
	fontChange_button->setBackGroundViewForState(CAControlStateHighlighted, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/design/font_change_select.png")));
	fontChange_button->setScale(0.5f);
	fontChange_button->setVisible(false);
	//设置监听
	fontChange_button->addTarget(this, CAControl_selector(DesignViewController::fontChangeCallbakc), CAControlEventTouchDown);
	this->getView()->addSubview(fontChange_button);

	//确认按钮
	Ok_button = CAButton::create(CAButtonTypeSquareRect);
	Ok_button->setCenter(DRect((Winsize.width)*0.94, (Winsize.height)*0.465, Winsize.width*0.1, Winsize.width*0.1));
	Ok_button->setTitleColorForState(CAControlStateNormal, CAColor_white);
	CAImageView* ok_image = CAImageView::createWithImage(CAImage::create("source_pro/design/Ok_button.png"));
	Ok_button->setBackGroundViewForState(CAControlStateNormal, ok_image);
	Ok_button->setBackGroundViewForState(CAControlStateHighlighted, ok_image);
	Ok_button->setScale(0.5f);
	Ok_button->setVisible(false);
	//设置监听
	Ok_button->addTarget(this, CAControl_selector(DesignViewController::OkCallback), CAControlEventTouchDown);
	this->getView()->addSubview(Ok_button);

	//键盘切换按钮
	keyChange_button = CAButton::create(CAButtonTypeSquareRect);
	keyChange_button->setCenter(DRect((Winsize.width)*0.86, (Winsize.height)*0.465, Winsize.width*0.1, Winsize.width*0.1));
	keyChange_button->setTitleColorForState(CAControlStateNormal, CAColor_white);
	keyChange_button->setBackGroundViewForState(CAControlStateNormal, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/design/keyboard_button.png")));
	keyChange_button->setBackGroundViewForState(CAControlStateHighlighted, CAScale9ImageView::createWithImage
		(CAImage::create("source_pro/design/keyboardselect_button.png")));
	keyChange_button->setScale(0.5f);
	keyChange_button->setVisible(false);
	//设置监听
	keyChange_button->addTarget(this, CAControl_selector(DesignViewController::KeyBoardCallback), CAControlEventTouchDown);
	this->getView()->addSubview(keyChange_button);

	//导航栏
	CANavigationBarItem* temp_nav = CANavigationBarItem::create(UTF8(""));
	CABarButtonItem* save_leftItem = CABarButtonItem::create(UTF8("< 返回"), NULL, NULL);
	save_leftItem->setItemWidth(110);
	CABarButtonItem* save_rightItem = CABarButtonItem::create(UTF8("保存 >"), NULL, NULL);
	save_rightItem->setItemWidth(110);

	temp_nav->setShowGoBackButton(false);
	temp_nav->addLeftButtonItem(save_leftItem);
	temp_nav->addRightButtonItem(save_rightItem);
	 
	save_leftItem->setTarget(this, CAControl_selector(DesignViewController::backCallBackDown));
	save_rightItem->setTarget(this, CAControl_selector(DesignViewController::saveCallBackDown));

	//CANavigationBar* tem_nav = CANavigationBar::create();
	CANavigationBar* tem_nav = CANavigationBar::createWithFrame(DRect(0, 0, Winsize.width, 98));
	CAImageView*img = CAImageView::createWithImage(CAImage::create("source_pro/up_navigationbar.png"));
	tem_nav->setBackgroundView(img);
	tem_nav->setItem(temp_nav);
	tem_nav->setVisible(true);
	this->getView()->insertSubview(tem_nav, 10);

	//加载图标
	loadingIcon = CAActivityIndicatorView::createWithFrame(DRect(0, 0 - 180, Winsize.width, Winsize.height));
	loadingIcon->setStyle(CAActivityIndicatorViewStyleGrayLarge);
	this->getView()->addSubview(loadingIcon);
	loadingIcon->stopAnimating();
}

//获取当前系统时间用于保存图片的名称数据
std::string DesignViewController::get_data_now()
{
	struct cc_timeval now;
	CCTime::gettimeofdayCrossApp(&now, NULL);
	struct tm *tm;
	time_t tp = now.tv_sec;
	tm = localtime(&tp);
	char date[30] = { 0 };
	sprintf(date, "jiumiao_%d%02d%02d%02d%02d%02d", (int)tm->tm_year + 1900,
		(int)tm->tm_mon + 1, (int)tm->tm_mday, (int)tm->tm_hour, (int)tm->tm_min, (int)tm->tm_sec);
	return std::string(date);
}

void DesignViewController::viewDidUnload()
{

}

//导航栏左边返回按钮回调函数
void DesignViewController::backCallBackDown(CAControl* control, CCPoint point)
{
	//CCLog(UTF8("返回回调函数"));
	//将该控制层移除栈顶
	RootWindow::getInstance()->getRootNavigationController()->popViewControllerAnimated(true);
}

//导航栏右边保存按钮回调函数
void DesignViewController::saveCallBackDown(CAControl* control, CCPoint point)
{
	//保存修改图片资源
	DSize Cellsize = showImageListview->_size;
	if (0 == isfilterView)
	{
		CARenderImage* screenShotd = CARenderImage::create(Cellsize.width, Cellsize.height);
		showImageListview->getSubviewByTag(86)->setDisplayRange(true);
		screenShotd->printscreenWithView(showImageListview, CCPoint(0, 0));

		//把图片写入到相册
		CADevice::writeToSavedPhotosAlbum(screenShotd->getImageView()->getImage(), get_data_now());

		//android
		#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
			ImageName = get_data_now();
		#endif

		//ios
		#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
			c_ImageName = screenShotd->getImageView()->getImage();
		#endif

		showImageListview->getSubviewByTag(86)->setDisplayRange(false);

	}

	else if (1 == isfilterView)
	{
		CARenderImage* screenShotd = CARenderImage::create(Cellsize.width, Cellsize.height);
		showImageListview->getSubviewByTag(86)->setDisplayRange(true);
		screenShotd->printscreenWithView(showImageListview, CCPoint(0, 0));

		//把图片写入到相册
		CADevice::writeToSavedPhotosAlbum(screenShotd->getImageView()->getImage(), get_data_now());

		//android
		#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
			ImageName = get_data_now();
		#endif

		//ios
		#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
			c_ImageName = screenShotd->getImageView()->getImage();
		#endif

		showImageListview->getSubviewByTag(86)->setDisplayRange(false);
	}
	else if (2 == isfilterView)
	{
		CARenderImage* screenShotd = CARenderImage::create(Cellsize.width, Cellsize.height);
		showImageListview->getSubviewByTag(86)->setDisplayRange(true);
		screenShotd->printscreenWithView(showImageListview, CCPoint(0, 0));

		//把图片写入到相册
		CADevice::writeToSavedPhotosAlbum(screenShotd->getImageView()->getImage(), get_data_now());
		//android
		#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
			ImageName = get_data_now();
		#endif
		//ios
		#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
			c_ImageName = screenShotd->getImageView()->getImage();
		#endif

		showImageListview->getSubviewByTag(86)->setDisplayRange(false);
	}
	CCSize winRect = this->getView()->getBounds().size;
	//隐藏原来导航栏
	RootWindow::getInstance()->getRootNavigationController()->setNavigationBarHidden(true, false);
	//跳转到保存页面视图
	ImgSaveFinishViewControler* imgSaveViewControler = new ImgSaveFinishViewControler();
	imgSaveViewControler->init();
	//弹出保存图片的viewcontrol
	RootWindow::getInstance()->getRootNavigationController()->pushViewController(imgSaveViewControler, true);

	imgSaveViewControler->release();
}

//文字切换按钮回调函数
void DesignViewController::fontChangeCallbakc(CAControl* btn, CCPoint point)
{
	//CCLog(UTF8("文字按钮会回调函数"));
	//按钮切换为键盘按钮
	fontChange_button->setVisible(false);
	keyChange_button->setVisible(true);
	//调用Sdk弹出选字体的页面
}

//确认按钮回调函数
void DesignViewController::OkCallback(CAControl* btn, CCPoint point)
{
	//CCLog(UTF8("确认按钮回调函数"));
	//保存当前图片的状态
}

//键盘按钮回调函数
void DesignViewController::KeyBoardCallback(CAControl* btn, CCPoint point)
{
	//CCLog(UTF8("键盘按钮回调函数"));
	//返回输入状态
	keyChange_button->setVisible(false);
	fontChange_button->setVisible(true);
}

void DesignViewController::segmentCallback(CASegmentedControl* btn, int index)
{
	CCLog("index== %d", index);
	//模板
	DSize WinSize = this->getView()->getBounds().size;
	if (0 == index)
	{
		this->flag = 0;
		this->getView()->removeSubview(filterlistview);
		this->getView()->removeSubview(picturelistview);
		templistview = CAListView::createWithFrame(DRect((WinSize.width)*0.03, (WinSize.height)*0.86, WinSize.width, (WinSize.height)*0.13));
		templistview->setListViewDelegate(this);
		templistview->setListViewDataSource(this);
		templistview->setAllowsSelection(true);
		templistview->setAllowsMultipleSelection(false);
		templistview->setListViewOrientation(CAListViewOrientationHorizontal);
		templistview->setShowsScrollIndicators(false);
		templistview->setSeparatorColor(CAColor_black);
		templistview->setBackgroundColor(CAColor_black);
		templistview->setSeparatorViewHeight(WinSize.width*0.018);
		this->getView()->addSubview(templistview);
	}
	//滤镜
	if (1 == index)
	{
		this->flag = 1;
		this->getView()->removeSubview(templistview);
		this->getView()->removeSubview(picturelistview);
		filterlistview = CAListView::createWithFrame(DRect((WinSize.width)*0.03, (WinSize.height)*0.86, WinSize.width, (WinSize.height)*0.13));
		filterlistview->setListViewDelegate(this);
		filterlistview->setListViewDataSource(this);
		filterlistview->setAllowsSelection(true);
		filterlistview->setAllowsMultipleSelection(false);
		filterlistview->setListViewOrientation(CAListViewOrientationHorizontal);
		filterlistview->setShowsScrollIndicators(false);
		filterlistview->setSeparatorColor(CAColor_black);
		filterlistview->setBackgroundColor(CAColor_black);
		filterlistview->setSeparatorViewHeight(WinSize.width*0.018);
		this->getView()->addSubview(filterlistview);
	}
	//构图
	if (2 == index)
	{
		this->flag = 2;
		this->getView()->removeSubview(templistview);
		this->getView()->removeSubview(filterlistview);
		picturelistview = CAListView::createWithFrame(DRect((WinSize.width)*0.08, (WinSize.height)*0.87, WinSize.width, (WinSize.width)*0.13)); //115

		picturelistview->setListViewDelegate(this);
		picturelistview->setListViewDataSource(this);
		picturelistview->setAllowsSelection(true);
		picturelistview->setAllowsMultipleSelection(false);
		picturelistview->setListViewOrientation(CAListViewOrientationHorizontal);
		picturelistview->setShowsScrollIndicators(false);
		picturelistview->setScrollEnabled(false);
		picturelistview->setSeparatorColor(CAColor_black);
		picturelistview->setBackgroundColor(CAColor_black);
		picturelistview->setSeparatorViewHeight(WinSize.width / 8);
		this->getView()->addSubview(picturelistview);
	}
}

void DesignViewController::listViewDidSelectCellAtIndex(CAListView *listView, unsigned int index)
{
	DSize Winsize = this->getView()->getBounds().size;
	//模板相关大图片资源
	if (0 == this->flag)
	{
		//先移除 方便缩放时没有移除
		this->getView()->removeSubview(showImageListview);
		//加载选中的大图片资源
		float num = Winsize.height*0.69 / 1.5;
		showImageListview = ShowSelectedImageListView::createWithFrame
			(DRect((Winsize.width - num) / 2, Winsize.height / 15, num, (Winsize.height)*0.69));

		sprintf(bigTemplateImage, "source_pro/design/%d.png", index);
		isfilterView = 0;
		this->getView()->addSubview(showImageListview);
	}

	//滤镜相关大图片资源
	else if (1 == this->flag)
	{
		//先移除 方便缩放时没有移除
		this->getView()->removeSubview(showImageListview);
		//加载选中的大图片资源
		float num = Winsize.height*0.69 / 1.5;
		showImageListview = ShowSelectedImageListView::createWithFrame
			(DRect((Winsize.width - num) / 2, Winsize.height / 15, num, (Winsize.height)*0.69));

		c_filter = index;
		isfilterView = 1;
		this->getView()->addSubview(showImageListview);
	}
	//对图片进行旋转操作
	else
	{
		//先移除 方便缩放时没有移除
		this->getView()->removeSubview(showImageListview);
		//加载选中的大图片资源
		float num = Winsize.height*0.69 / 1.5;
		showImageListview = ShowSelectedImageListView::createWithFrame
			(DRect((Winsize.width - num) / 2, Winsize.height / 15, num, (Winsize.height)*0.69));

		selectType = index;
		isfilterView = 2;
		this->getView()->addSubview(showImageListview);
	}
}

void DesignViewController::listViewDidDeselectCellAtIndex(CAListView *listView, unsigned int index)
{
	if (0 == this->flag)
	{
		this->getView()->removeSubview(showImageListview);
	}
}

unsigned int DesignViewController::numberOfIndex(CAListView *listView)
{
	if (2 == this->flag)
	{
		return 4;
	}
	else if (1 == this->flag)
	{
		return 8;
	}
	else
	{
		return templateList.size();
	}
}

unsigned int DesignViewController::listViewHeightForIndex(CAListView *listView, unsigned int index)
{
	DSize Winsize = this->getView()->getBounds().size;
	if (2 == this->flag)
	{
		//115
		return (Winsize.width)*0.12;
	}
	else
	{
		return (Winsize.height)*0.13 / 1.5;
	}
}

CAListViewCell* DesignViewController::listViewCellAtIndex(CAListView *listView, const CCSize& cellSize, unsigned int index)
{
	DSize _size = cellSize;
	TemplateLisViewCell* cell = NULL;
	PictureLisViewCell* picturCell = NULL;
	FilterLisViewCell* filterCell = NULL;

	//通过重用标示符获得内存中已有的Cell  相当于Tag
	if (0 == this->flag || 1 == this->flag)
	{
		cell = (TemplateLisViewCell*)listView->dequeueReusableCellWithIdentifier("ListViewCell");
		if (!picturCell)
		{
			cell = TemplateLisViewCell::create("ListViewCell_"); 
			CAImageView* im = NULL;
			switch (this->flag)
			{
			case 0:
			{
					  im = CAImageView::createWithImage(CAImage::create(templateList.at(index)));
					  im->setCenter(DRect(_size.width*0.5, _size.height*0.5, _size.width - 10, _size.height - 10));
					  cell->addSubview(im);
					  break;
			}
			case 1:
			{
					  CAView* filterView = CAView::create();
					  filterView->setCenter(DRect(_size.width*0.5, _size.height*0.5, _size.width - 10, _size.height - 10));

					  //效果说明lable
					  CALabel* label = CALabel::createWithCenter(DRect(_size.width*0.45, _size.height*0.85, _size.width*0.7, _size.height*0.3));

					  label->setText(filterLable.at(index));
					  label->setColor(CAColor_white);
					  label->setFontSize(24);
					  label->setTextAlignment(CATextAlignmentCenter);
					  label->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
					  filterView->insertSubview(label, 2);

					  CAImage* templateImg_scale = CAImage::scaleToNewImageWithImage(CAImage::create(bigTemplateImage), DSize(_size.width, _size.height));
					  //设置相应的滤镜效果
					  CAImageView* templateImg = CAImageView::createWithImage(templateImg_scale);
					  templateImg->setFrame(DRect(0, 0, _size.width - 10, _size.height - 10));
					  filterView->insertSubview(templateImg, 1);

					  CAImage* changeImg_scale = CAImage::scaleToNewImageWithImage(UserSelectImg.at(0), DSize(_size.width, _size.height));
					  //设置相应的滤镜效果
					  CAImage* changeImg_filterEffect = this->getFilterEffect(changeImg_scale, index);

					  CAImageView* changeImg = CAImageView::createWithImage(changeImg_filterEffect);
					  changeImg->setFrame(DRect(0, 0, _size.width - 10, _size.height - 10));

					  //渐变图片
					  CAImageView* wearImg = CAImageView::createWithImage(CAImage::create("source_pro/design/wear.png"));
					  wearImg->setFrame(DRect(0, 0, _size.width - 10, _size.height - 10));

					  filterView->insertSubview(changeImg, 0);
					  filterView->insertSubview(wearImg, 1);


					  cell->addSubview(filterView);
					  break;
			}
			default:
				break;
			}
		}
		return cell;
	}

	else
	{
		//PictureLisViewCell
		picturCell = (PictureLisViewCell*)listView->dequeueReusableCellWithIdentifier("ListViewCells");
		if (!picturCell)
		{
			picturCell = PictureLisViewCell::create("ListViewCells_");
			CAImageView* im = CAImageView::createWithImage(CAImage::create(pictureList.at(index)));
			im->setCenter(DRect(_size.width*0.5, _size.height*0.5, _size.width - 3, _size.height - 3));

			picturCell->addSubview(im);
		}
		return picturCell;
	}
}

//设置滤镜效果
CAImage *DesignViewController::getFilterEffect(CAImage *var, unsigned int filteType)
{
	CAImage::PixelFormat pixelFormat = var->getPixelFormat();
	unsigned int pixelsWide = var->getPixelsWide();
	unsigned int pixelsHigh = var->getPixelsHigh();
	//图片数据的长度
	unsigned long dataLenght = var->getDataLenght();
	//图片数据的大小
	unsigned char* data = static_cast<unsigned char*>(malloc(dataLenght * sizeof(unsigned char)));
	memcpy(data, var->getData(), dataLenght);

	if (pixelFormat == CAImage::PixelFormat_RGBA8888 || pixelFormat == CAImage::PixelFormat_RGBA4444)
	{

		switch (filteType)
		{
			case 0:
				getFilters(data, data, pixelsWide, pixelsHigh, OriginImage, 0);
				break;

			case 1:
				getFilters(data, data, pixelsWide, pixelsHigh, Classics, 0);
				break;

			case 2:
				getFilters(data, data, pixelsWide, pixelsHigh, BlackAndWhite, 0);
				break;

			case 3:
				getFilters(data, data, pixelsWide, pixelsHigh, Cloud, 0);
				break;

			case 4:
				getFilters(data, data, pixelsWide, pixelsHigh, MigratoryBird, 0);
				break;

			case 5:
				getFilters(data, data, pixelsWide, pixelsHigh, PhotographicPlate, 0);
				break;

			case 6:
				getFilters(data, data, pixelsWide, pixelsHigh, Relief, 0);
				break;

			case 7:
				getFilters(data, data, pixelsWide, pixelsHigh, QuietlyElegant, 0);
				break;

			default:
				break;
		}
	}
	else
	{
		switch (filteType)
		{
		case 0:
			getFilters(data, data, pixelsWide, pixelsHigh, OriginImage, 1);
			break;

		case 1:
			getFilters(data, data, pixelsWide, pixelsHigh, Classics, 1);
			break;

		case 2:
			getFilters(data, data, pixelsWide, pixelsHigh, BlackAndWhite, 1);
			break;

		case 3:
			getFilters(data, data, pixelsWide, pixelsHigh, Cloud, 1);
			break;

		case 4:
			getFilters(data, data, pixelsWide, pixelsHigh, MigratoryBird, 1);
			break;

		case 5:
			getFilters(data, data, pixelsWide, pixelsHigh, PhotographicPlate, 1);
			break;

		case 6:
			getFilters(data, data, pixelsWide, pixelsHigh, Relief, 1);
			break;

		case 7:
			getFilters(data, data, pixelsWide, pixelsHigh, QuietlyElegant, 1);
			break;

		default:
			break;
		}
	}

	isShowLodingIcon = true;
	return CAImage::createWithRawDataNoCache(data, pixelFormat, pixelsWide, pixelsHigh);
}


/*
类：  PictureLisViewCell
父类：CAListViewCell
*/

PictureLisViewCell::PictureLisViewCell()
{

}

PictureLisViewCell::~PictureLisViewCell()
{

}

void PictureLisViewCell::selectedListViewCell()
{
	this->setBackgroundView(CAView::createWithColor(CAColor_black));
	unsigned short index_ = this->getIndex();
	switch (index_)
	{
	case 0:
		this->removeSubviewByTag(100);
		if (0 == rotionAnagle % 180)
		{
			//isSetRotionWith = true;
		}
		break;
	case 1:
		this->removeSubviewByTag(200);
		if (0 == rotionAnagle % 180)
		{
			//isSetRotionWith = true;
		}
		break;
	case 2:
		this->removeSubviewByTag(300);
		break;
	case 3:
		this->removeSubviewByTag(400);
		break;
	default:
		break;
	}
	this->removeSubviewByTag(100);

}



void PictureLisViewCell::normalListViewCell()
{
	this->setBackgroundView(CAView::createWithColor(CAColor_black));
}


void PictureLisViewCell::highlightedListViewCell()
{
	unsigned short index_ = this->getIndex();
	DSize _size = this->getBounds().size;
	switch (index_)
	{
	case 0:
	{
			  CAImageView* backward_image = CAImageView::createWithImage(CAImage::create("source_pro/design/backward_select.png"));
			  backward_image->setCenter(DRect(_size.width*0.5, _size.height*0.5, _size.width - 3, _size.height - 3));
			  backward_image->setTag(100);
			  this->insertSubview(backward_image, 0);
	}
		break;
	case 1:
	{
			  CAImageView* clockwise_image = CAImageView::createWithImage(CAImage::create("source_pro/design/clockwise_select.png"));
			  clockwise_image->setCenter(DRect(_size.width*0.5, _size.height*0.5, _size.width - 3, _size.height - 3));
			  clockwise_image->setTag(200);
			  this->insertSubview(clockwise_image, 0);
	}
		break;
	case 2:
	{
			  CAImageView* updown_image = CAImageView::createWithImage(CAImage::create("source_pro/design/updown_select.png"));
			  updown_image->setCenter(DRect(_size.width*0.5, _size.height*0.5, _size.width - 3, _size.height - 3));
			  updown_image->setTag(300);
			  this->insertSubview(updown_image, 0);

	}
		break;
	case 3:
	{
			  CAImageView* leftright_image = CAImageView::createWithImage(CAImage::create("source_pro/design/leftright_select.png"));
			  leftright_image->setCenter(DRect(_size.width*0.5, _size.height*0.5, _size.width - 3, _size.height - 3));
			  leftright_image->setTag(400);
			  this->insertSubview(leftright_image, 0);
	}
		break;

	default:
		break;
	}
}

void PictureLisViewCell::disabledListViewCell()
{

}

PictureLisViewCell* PictureLisViewCell::create(const std::string& reuseIdentifier)
{
	PictureLisViewCell* tableViewCell = new PictureLisViewCell();
	if (tableViewCell&&tableViewCell->initWithReuseIdentifier(reuseIdentifier))
	{
		tableViewCell->autorelease();
		return tableViewCell;
	}
	CC_SAFE_DELETE(tableViewCell);
	return NULL;
}

/*
类：  TemplateLisViewCell
父类：CAListViewCell
*/

TemplateLisViewCell::TemplateLisViewCell()
{

}

TemplateLisViewCell::~TemplateLisViewCell()
{

}

//正常状态
void TemplateLisViewCell::normalListViewCell()
{
	//移除前一个被选中的
	this->removeSubviewByTag(100);
	//列表中的每个cell的背景颜色设置为透明色
	this->setBackgroundView(CAView::createWithColor(CAColor_clear));
}


//高亮状态
void TemplateLisViewCell::highlightedListViewCell()
{

}

//选择状态
void TemplateLisViewCell::selectedListViewCell()
{
	//添加选中边框   
	CAImageView* backward_image = CAImageView::createWithImage(CAImage::create("source_pro/design/selected_frame.png"));
	DRect winSize = this->getBounds();
	//backward_image->setFrame(DRect(-5, -5, winSize.getMaxX() + 10, winSize.getMaxY() + 10));
	backward_image->setFrame(DRect(0, 0, winSize.getMaxX(), winSize.getMaxY()));

	backward_image->setTag(100);
	this->insertSubview(backward_image, 0);
}

//禁用状态
void TemplateLisViewCell::disabledListViewCell()
{

}

TemplateLisViewCell* TemplateLisViewCell::create(const std::string& reuseIdentifier)
{
	TemplateLisViewCell* tableViewCell = new TemplateLisViewCell();
	if (tableViewCell&&tableViewCell->initWithReuseIdentifier(reuseIdentifier))
	{
		tableViewCell->autorelease();
		return tableViewCell;
	}
	CC_SAFE_DELETE(tableViewCell);
	return NULL;
}


/*
类：  FilterLisViewCell
父类：CAListViewCell
*/
FilterLisViewCell::FilterLisViewCell()
{

}
FilterLisViewCell::~FilterLisViewCell()
{

}

FilterLisViewCell* FilterLisViewCell::create(const std::string& reuseIdentifier)
{
	FilterLisViewCell* tableViewCell = new FilterLisViewCell();
	if (tableViewCell&&tableViewCell->initWithReuseIdentifier(reuseIdentifier))
	{
		tableViewCell->autorelease();
		return tableViewCell;
	}
	CC_SAFE_DELETE(tableViewCell);
	return NULL;
}

//正常状态
void FilterLisViewCell::normalListViewCell()
{
	this->removeSubviewByTag(100);
}
//高亮状态
void FilterLisViewCell::highlightedListViewCell()
{

}
//选择状态
void FilterLisViewCell::selectedListViewCell()
{
	//添加选中边框
	CAImageView* backward_image = CAImageView::createWithImage(CAImage::create("source_pro/design/selected_frame.png"));
	DRect winSize = this->getBounds();
	backward_image->setFrame(DRect(-5, -5, winSize.getMaxX() + 10, winSize.getMaxY() + 10));
	backward_image->setTag(100);
	this->insertSubview(backward_image, 0);
}

//禁用状态
void FilterLisViewCell::disabledListViewCell()
{

}


/*
类：  TemplateLisViewCell
父类：CAListViewCell
*/

ShowSelectedImageListViewCell::ShowSelectedImageListViewCell()
{

}

ShowSelectedImageListViewCell::~ShowSelectedImageListViewCell()
{

}


ShowSelectedImageListViewCell* ShowSelectedImageListViewCell::create(const std::string& reuseIdentifier)
{
	ShowSelectedImageListViewCell* tableViewCell = new ShowSelectedImageListViewCell();
	if (tableViewCell&&tableViewCell->initWithReuseIdentifier(reuseIdentifier))
	{
		tableViewCell->autorelease();
		return tableViewCell;
	}
	CC_SAFE_DELETE(tableViewCell);
	return NULL;
}

//正常状态
void ShowSelectedImageListViewCell::normalListViewCell()
{
	this->setBackgroundView(CAView::createWithColor(CAColor_clear));
}

//高亮状态
void ShowSelectedImageListViewCell::highlightedListViewCell()
{

}

//选择状态
void ShowSelectedImageListViewCell::selectedListViewCell()
{
	//缩放视图
	this->setScaleY(0.65f);
	this->setBackgroundView(CAView::createWithColor(CAColor_clear));
}

//禁用状态
void ShowSelectedImageListViewCell::disabledListViewCell()
{

}

/*
显示选中的加载大的图片
class：ShowSelectedImageListView
*/

ShowSelectedImageListView::ShowSelectedImageListView()
{

}

ShowSelectedImageListView::~ShowSelectedImageListView()
{
	//delete this->m_drawPthread;
}


bool ShowSelectedImageListView::init(const CrossApp::CCRect &rect)
{
	DSize size = rect.size;
	//CAListView *listView = CAListView::createWithCenter(CADipRect(size.width / 2+70, size.height / 2-20, size.width, size.height));
	CAListView *listView = CAListView::createWithFrame(DRect(0, 0, size.width, size.height));
	listView->setListViewDelegate(this);
	listView->setListViewDataSource(this);
	listView->setAllowsSelection(true);
	listView->setAllowsMultipleSelection(false);
	listView->setListViewOrientation(CAListViewOrientationHorizontal);
	listView->setShowsScrollIndicators(false);
	listView->setScrollEnabled(false);
	listView->setSeparatorColor(CAColor_clear);
	listView->setBackgroundColor(CAColor_clear);
	listView->setSeparatorViewHeight(0);
	this->addSubview(listView);

	return true;
}

ShowSelectedImageListView* ShowSelectedImageListView::createWithFrame(const CCRect& rect)
{
	ShowSelectedImageListView *pRet = new ShowSelectedImageListView();
	if (pRet && pRet->init(rect))
	{
		pRet->setFrame(rect);
		pRet->autorelease();
		return pRet;
	}
	CC_SAFE_DELETE(pRet);
	return NULL;
}

void ShowSelectedImageListView::listViewDidSelectCellAtIndex(CAListView *listView, unsigned int index)
{
	//this->setFrame(CADipRect(0, _size.height - 715, 0, 0));
	this->setFrame(DRect(_size.width*0.23, (_size.height)*(-0.17), 0, 0));

	//设置缩放后背景的颜色为透明
	listView->setBackgroundColor(CAColor_clear);

	//显示出输入相关视图
	//inputColor->setVisible(true);
	//textField->setVisible(true);
	//Ok_button->setVisible(true);
	//fontChange_button->setVisible(true);
	//lable->setVisible(true);
}


void ShowSelectedImageListView::listViewDidDeselectCellAtIndex(CAListView *listView, unsigned int index)
{
	int num = index;
}

unsigned int ShowSelectedImageListView::numberOfIndex(CAListView *listView)
{
	return 1;
}

unsigned int ShowSelectedImageListView::listViewHeightForIndex(CAListView *listView, unsigned int index)
{
	//水平方向
	DSize size = RootWindow::getInstance()->getBounds().size;
	if (true == isSetRotionWith)
	{
		return size.width;
	}
	else
	{
		//return size.width*0.83;
		return size.height*0.69 / 1.5;  //模板图片的比例值为 2：3
	}
}


void ShowSelectedImageListView::tick(float dt)
{
	if (true == isShowLodingIcon)
	{
		loadingIcon->stopAnimating();
		CAScheduler::unschedule(schedule_selector(ShowSelectedImageListView::tick), this);
	}
}

CAListViewCell* ShowSelectedImageListView::listViewCellAtIndex(CAListView *listView, const CCSize& cellSize, unsigned int index)
{
	//inputColor->setVisible(false);
	//textField->setVisible(false);
	//Ok_button->setVisible(false);
	//fontChange_button->setVisible(false);
	//lable->setVisible(false);
	//keyChange_button->setVisible(false);

	//CADipSize _size = cellSize;  
	_size = cellSize;
	static bool isfile = false;
	ShowSelectedImageListViewCell* cell = (ShowSelectedImageListViewCell*)listView->dequeueReusableCellWithIdentifier("ListViewCell");
	if (!cell)
	{ 
		cell = ShowSelectedImageListViewCell::create("ListViewCell_");
		DSize S_size;
		//显示模板效果大图片资源

		//创建 ScrollView
		scrollView = CAScrollView::createWithFrame(DRect(0, 0, _size.width, _size.height));
		S_size = scrollView->getBounds().size;
		scrollView->setTag(86);
		scrollView->setViewSize(DSize(S_size.width, S_size.height));
		scrollView->setMinimumZoomScale(0.4f);
		scrollView->setMaximumZoomScale(4.0f);
		scrollView->setBounceHorizontal(false); 
		scrollView->setBounceVertical(false);
		scrollView->setShowsScrollIndicators(false);
		scrollView->setMultitouchGesture(CAScrollView::ZoomAndRotate);
		this->insertSubview(scrollView, 0);

		//设置代理 
		//scrollView->setScrollViewDelegate(this);
		//创建大图片模板
		CAImage* m_bkImage = CAImage::create(bigTemplateImage);
		CAImageView* im = CAImageView::createWithImage(m_bkImage);
		im->setTag(index);
		im->setFrame(DRect(0, 0, _size.width, _size.height));
		//设置最上层的模板层不可以触摸， scrollView修改图片层可以触摸
		im->setTouchEnabled(false);
		this->insertSubview(im, 1);

		if (0 == isfilterView)
		{
			//滚动视图
			//CAImageView* changImage = CAImageView::createWithImage(CAImage::create(changeImagePath));
			CAImageView* changImage = CAImageView::createWithImage(UserSelectImg.at(0));
			changImage->setFrame(DRect(0, 0, S_size.height*scale_value, S_size.height));
			scrollView->addSubview(changImage);

			isfile = false;
		}
		//显示滤镜效果大图片资源
		else if (1 == isfilterView)
		{
			loadingIcon->startAnimating();
			CAScheduler::schedule(schedule_selector(ShowSelectedImageListView::tick), this, 0.5);
			/*
				滤镜效果
			*/
			CAImage* m_changeFilterImage = DesignViewController::getFilterEffect(UserSelectImg.at(0), c_filter);
			CAImageView* changImage = CAImageView::createWithImage(m_changeFilterImage);
			changImage->setFrame(DRect(0, 0, S_size.height*scale_value, S_size.height));
			scrollView->addSubview(changImage);
			isfile = true;
		}
		//旋转图片
		else
		{
			//图片构图
			if (true == isfile)
			{
				CAImage* m_changeFilterImage = DesignViewController::getFilterEffect(UserSelectImg.at(0), c_filter);
				CAImageView* changImage = CAImageView::createWithImage(m_changeFilterImage);
				changImage->setFrame(DRect(0, 0, S_size.height*scale_value, S_size.height));

				scrollView->addSubview(changImage);

				static bool  isUpDownFilp = true;
				static bool  isLeftRightFilp = true;

				switch (selectType)
				{
					//逆时针旋转90
					case 0:
					{
						  im->setFlipY(false);
						  changImage->setFlipY(false);

						  if (rotionAnagle >= 360){ rotionAnagle = 0; }
						  rotionAnagle = rotionAnagle + 90;
						  changImage->setRotation(-(rotionAnagle));
					}
					break;

					//顺时针旋转90
					case 1:
					{
						  im->setFlipY(false);
						  changImage->setFlipY(false);

						  if (rotionAnagle >= 360){ rotionAnagle = 0; }
						  rotionAnagle = rotionAnagle + 90;
						  changImage->setRotation(rotionAnagle);
					}
					break;

					//上下翻滚
					case 2:
					{
						  if (true == isUpDownFilp)
						  {
							  changImage->setFlipY(true);
							  isUpDownFilp = false;
						  }
						  else
						  {
							  changImage->setFlipY(false);
							  isUpDownFilp = true;
						  }
					}
					break;

					//左右翻滚
					case 3:
					{
						  if (true == isLeftRightFilp)
						  {
							  changImage->setFlipX(true);
							  isLeftRightFilp = false;
						  }
						  else
						  {
							  changImage->setFlipX(false);
							  isLeftRightFilp = true;
						  }
					}
					break;

				default:
					break;
				}
			}

			//没有滤镜效果
			else
			{
				CAImageView* changImage = CAImageView::createWithImage(UserSelectImg.at(0));
				changImage->setFrame(DRect(0, 0, S_size.height*scale_value, S_size.height));
				scrollView->addSubview(changImage);

				static bool  isUpDownFilp = true;
				static bool  isLeftRightFilp = true;

				switch (selectType)
				{
					//逆时针旋转90（先缩放后旋转）
				case 0:
				{
						  im->setFlipX(false);
						  changImage->setFlipX(false);

						  if (rotionAnagle >= 360){ rotionAnagle = 0; }
						  rotionAnagle = rotionAnagle + 90;
						  //scrollView->setInterRacation(-(rotionAnagle));
						  changImage->setRotation(-(rotionAnagle));

				}
					break;

					//顺时针旋转90
				case 1:
				{
						  im->setFlipX(false);
						  changImage->setFlipX(false);

						  if (rotionAnagle >= 360){ rotionAnagle = 0; }
						  rotionAnagle = rotionAnagle + 90;
						 // scrollView->setInterRacation(rotionAnagle);
						  changImage->setRotation(rotionAnagle);
				}
					break;

					//上下翻滚
				case 2:
				{
						  if (true == isUpDownFilp)
						  {
							  changImage->setFlipY(true);
							  isUpDownFilp = false;
						  }
						  else
						  {
							  changImage->setFlipY(false);
							  isUpDownFilp = true;
						  }
				}
					break;

					//左右翻滚
				case 3:
				{
						  if (true == isLeftRightFilp)
						  {
							  changImage->setFlipX(true);
							  isLeftRightFilp = false;
						  }
						  else
						  {
							  changImage->setFlipX(false);
							  isLeftRightFilp = true;
						  }
				}
					break;

				default:
					break;
				}

			}

		}

	}
	return cell;
}

//多线程回调函数
void* ShowSelectedImageListView::draw_pthread(void* funPro)
{
	return 0;
}






